<?php
// {{{ includes
load_class('Controller_FoBase');

load_file(APP_DIR.'/config.inc.php');
load_file(APP_DIR.'/client/client.php');

// }}}

/**
 * 定义Controller_User
 *
 * @author jack
 * @todo 前台用户管理控制器 
 */


class Controller_User extends Controller_FoBase {
	
	var $_modelUser;
	
	function Controller_User(){
		$this->_modelUser=get_singleton('Model_User');
	}
	
	
	function actionIndex(){	
		
		$this->isLogin();
			
		$user=$this->_modelUser->getUser();
		
		$orders=$this->MyOrders();
		
		$_modelAccount = get_singleton('Model_Account');
		$rowset=$_modelAccount->find('uid='.$_SESSION['USERID']); 
		$balance=$rowset['balance']==''?'0':$rowset['balance'];
				
		include(FRONTEND_TPL_DIR.'/user/index.tpl.php');	
	}
	
	
	/**
	 * 我的订单 
	 *
	 */
	function MyOrders(){
		
		$_moderActivityOrder = get_singleton('Model_ActivityOrder');
		$conditions = 'uid='.$_SESSION['USERID'];
		
		if($_GET['pay_status'] == 'Y'){
			$conditions.=' and pay_status="Y"';
		}
		elseif ($_GET['pay_status'] == 'N'){
			$conditions.=' and pay_status="N"';
		}
		
		
		$page=(int)$_GET['page'];
		$pageSize=10;
		$sort='order_id desc';
		
		$_moderActivityOrder->enableLink('summary');
		$orders=$_moderActivityOrder->getList($conditions,$page,$pageSize,$sort);
		  
		if($orders['recoders']){
			$_moderActivity = get_singleton('Model_Activity');
			foreach($orders['recoders'] as $key => $val){
				$activity = $_moderActivity->find($val['activity_id'],null,'activity_id,title');
				$rowset[]=array('order_id' => $val['order_id'],
								'pay_status' => $val['pay_status'],
								'process_status' => $val['process_statys'],
								'activity_id' => $activity['activity_id'],
								'title' => $activity['title'],
								'price' => $val['summary'][0]['price'],
								'qty' => $val['summary'][0]['qty'],
								'is_shipping' => $val['is_shipping'],
								'shipping_price' => $val['shipping_price'],
								
								);
			}
		}
		
		$array=array('recoders' => $rowset,'PageBar' => $orders['PageBar']);
		return $array;
		
	}
	
	/**
	 * 账号设置
	 *
	 */
	function actionProfile(){
		
		$rowset=array('username' => $_SESSION['USERNAME'],'password' => $_POST['password']);
		$this->_modelUser->Update($_SESSION['USERID'],$rowset);
		
		$url=url('User','',array('tab' => 2));
		redirect($url);
	}
	
	
	
	/**
	 * 实现用户登录，同时登录到discuz
	 *
	 */
	function actionLogin(){	 
		
		if($_POST['username'] == '' || $_POST['password'] == '') {
			echo '';
			 
		} else {
			$username=trim($_POST['username']);
			$password=trim($_POST['password']);
			$return=$this->_modelUser->Login($username,$password);
			
			$user=$this->_modelUser->getUser();
			
			$_SESSION['USERID']=$user['uid'];
			$_SESSION['USERNAME']=$user['username'];
			echo $user["username"];		//output username
			 
		}
		
	}
	function actionLogout(){
		
		$this->_modelUser->logout();
		echo '<script language="javascript">setTimeout("document.location.href=\'./\'",200)</script>';
	}
	
	function isLogin(){
		if($_SESSION['USERID'] == '' || $_SESSION['USERNAME'] == ''){
			js_alert('请您登录！','',url('',''));
			exit;
		}
		else{
			return true;
		}
	}
	
	function actionRegister(){
		 
		if($_POST['is_post']){
			
			if($_POST['username'] == ''){
				echo "请输入您的用户名.";
				return  false;
			}
			if($_POST['password'] == ''){
				echo '请输入您的密码.';
				return false;
			}
			if($_POST['email'] == ''){
				echo '请输入您的邮箱.';
				return false;
			}
			
		//if($_POST['username'] && $_POST['password'] && $_POST['email'] && $_POST['mobile']){
			
			
			$imgcode =& FLEA::getSingleton('FLEA_Helper_ImgCode');
	      // 假定验证码在表单中的字段名是 imgcode
	      if (!$imgcode->check($_POST['code'])) {
	          echo '您输入的验证码不正确.';
	          return false;
	      }
			//--检查用户是否存在
			$conditions='username="'.$_POST['username'].'"';
			$user=$this->_modelUser->find($conditions);
			
			if($user){
				echo '您注册的用户名已经被占用，请选择其他用户名。';
				return false;
				
			}
			
			//--检查邮箱是否存在
			$conditions='email="'.$_POST['email'].'"';
			$user=$this->_modelUser->find($conditions);
			if($user){
				echo '您填写的Email已经存在，请选择其他Email。';
				return false;
				
			}
			
			$rowset=array(	'username' => strip_tags($_POST['username']),
							'password' => $_POST['password'],
							'email' => strip_tags($_POST['email']),
							'mobile' => strip_tags($_POST['mobile']),
							'invites' => $_POST['invites'],
					);		
					
			$uid=$this->_modelUser->Register($rowset);
			
			//--如果订阅邮件列表
			$_modelNewsLetter = get_singleton('Model_NewsLetter');
			$rowset=array('email' => $_POST['email']);
			$_modelNewsLetter->save($rowset);
			
			$_SESSION['USERID'] = $uid;
			$_SESSION['USERNAME'] = strip_tags($_POST['username']);	
			echo uc_user_synlogin($uid);
			echo strip_tags($_POST['username']);	
			//echo "注册成功！请登录。";
		
		}else{
			
				$this->_Register();
		}
		 
	}
	
	function _Register($rowset,$error){
		
		include(FRONTEND_TPL_DIR.'/register.tpl.php');
	}
	
		
	function actionImgcode() {
      $imgcode =& FLEA::getSingleton('FLEA_Helper_ImgCode');
      $imgcode->image();
  	}
	
	

	function _Login($rowset,$error){
		//登录表单
		echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'?controller=User&action=login">';
		echo '登录:';
		echo '<dl><dt>用户名</dt><dd><input name="username"></dd>';
		echo '<dt>密码</dt><dd><input name="password" type="password"></dd></dl>';
		echo '<input name="submit" type="submit"> ';
		echo '</form>';
	}
}



